<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<div class="layui-fluid">
  <table class="layui-hide" id="empInfo" lay-filter="empInfo"></table>
</div>

<script id="toolbar"  type="text/html">

    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>

    <button class="layui-btn layui-btn-sm" lay-event="add">添加请假记录</button>
     <div class="layui-inline">
     <div class="layui-input-inline" style="width:100px;height:38px;">
       <input type="text" name="likeEmpNo" id="likeEmpNo" required lay-verify="required" placeholder="请输入工号" class="layui-input">
     </div>
     
     <div class="layui-inline">
       <button class="layui-btn layui-btn-sm"  data-type="reload" id="search"><i class="layui-icon">&#xe615;</i>搜索</button>
     </div>  
    </div>
</script>

<script id="bar" type="text/html">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="editLayer">
    <!-- 编辑模态框 -->
    <div>
     <form action="/edit" class="layui-form" id="editForm">
     <div class="layui-form-item" style="display:none;">
       <label class="layui-form-label">id</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="id" id="id">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">工号</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="empNo" id="empNo">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">姓名</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="userName" id="userName">
       </div>   
     </div>
    <div class="layui-form-item">
       <label class="layui-form-label">请假类型</label>
       <div class="layui-input-inline">
    
           <select name="type" id="type">
             <option value="事假">事假</option>
             <option value="病假">病假</option>
             <option value="婚假">婚假</option>
             <option value="丧假">丧假</option>
             <option value="出差">出差</option>
           </select>
       </div>   
     </div>
     <div class="layui-form-item">
      <div class="layui-block">
        <label class="layui-form-label">时间</label>
        <div class="layui-input-inline" style="width:100px;">
          <input class="layui-input" type="text" name="starTime" id="starTime" placeholder="开始日期" lay-verify="required">
        </div>
        <div class="layui-form-mid">--</div>
        <div class="layui-input-inline" style="width:100px;">
          <input class="layui-input" type="text" name="endTime" id="endTime" placeholder="结束日期" lay-verify="required">
        </div>
      </div>
      </div>
     </form> 
    </div>
</script>

<script type="text/html" id="addLayer">
    <!-- 添加模态框 -->
    <div>
     <form action="/edit" class="layui-form" id="editForm">
     <div class="layui-form-item" style="display:none;">
       <label class="layui-form-label">id</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="id" id="id">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">工号</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="empNo" id="empNo">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">姓名</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="userName" id="userName">
       </div>   
     </div>
    <div class="layui-form-item">
       <label class="layui-form-label">请假类型</label>
       <div class="layui-input-inline">
    
           <select name="type" id="type">
             <option value="">-请选择-</option>
             <option value="事假">事假</option>
             <option value="病假">病假</option>
             <option value="婚假">婚假</option>
             <option value="丧假">丧假</option>
             <option value="出差">出差</option>
           </select>
       </div>   
     </div>
     <div class="layui-form-item">
      <div class="layui-block">
        <label class="layui-form-label">时间</label>
        <div class="layui-input-inline" style="width:100px;">
          <input class="layui-input" type="text" name="starTime" id="starTime" placeholder="开始日期" lay-verify="required">
        </div>
        <div class="layui-form-mid">--</div>
        <div class="layui-input-inline" style="width:100px;">
          <input class="layui-input" type="text" name="endTime" id="endTime" placeholder="结束日期" lay-verify="required">
        </div>
      </div>
      </div>
     </form> 
    </div>
</script>

<script src="layui/layui.js"></script> 
<script th:inline="none">
  layui.use('table',function(){
	  var table=layui.table;
	  var $=layui.jquery;
	  table.render({
		  elem:'#empInfo',
		  url:'/getAllVacate',
		  method:'post',
		  toolbar:'#toolbar',
		  defaultToolbar:['filter','exports','print',{title:'提示',layEvent:'LAYTABLE_TIPS',icon:'layui-icon-tips'}],
		  title:'员工信息表',
		  cols:[[
		    	 {type:'checkbox',fixed:'left'},
		    	 {field:'id',title:'ID',width:80,fixed:'left',unresize:true,sort:true}
		    	 ,{field:'empNo',title:'工号',width:120,sort:true}
		    	 ,{field:'userName', title:'姓名',width:120,sort: true}
		    	 ,{field:'type', title:'请假类型',width:120}
		    	 ,{field:'starTime', title:'开始时间'}		    	
		    	 ,{field:'endTime', title:'结束时间'}	
		    	 ,{field:'status',title:'状态',templet:function(d){
		    		 if(d.status==1){
		    			 return '<span style="color:#008000;">' + '已批准' + '</span>'
		    		 }
		    		 if(d.status==0){
		    			 return '<span style="color:#F44336;">' + '未批准' + '</span>'
		    		 }
		    		 if(d.status==2){
		    			 return '<span style="color:#F44336;">' + '不同意' + '</span>'
		    		 }
		    	 }}
		    	 ,{fixed: 'right', title:'操作', toolbar: '#bar', width:140}
		     ]],
		     id:'testreload',
		     page:true
	  });
	  

	  //表格数据重载
	  var active={
			  reload:function(){
				  var likeEmpNo=$('#likeEmpNo');
				//  var likeDate=$('#likeDate');
				  table.reload('testreload',{
					  page:{
						  curr:1
					  },
					  where:{
						  //参数name的名称要与控制器中的一致
						  empNo:likeEmpNo.val(),
					  },
					  url:'/searchVacateByKeyWord',
					  method:'post'
				  });
			  }
	  };
	  
	  //搜索按钮事件
	  
	  $("#search").on('click',function(){
		  var type=$(this).data('type');
		  active[type]?active[type].call(this):'';
	  });
	  
	  
	  //头部工具栏
	  table.on('toolbar(empInfo)',function(obj){
		  var checkStatus=table.checkStatus(obj.config.id);
		  switch(obj.event){
		  case 'add':
			 // var data=checkStatus.data;
			//  layer.alert(JSON.stringify(data));
			  layui.use('form',function(){
				  var form =layui.form;
				  var $=layui.jquery;
				
				  var index=layer.open({
					  formType:1,
					  title:'添加请假信息',
					  area:['450px','380px'],
					  content:$('#addLayer').html(),
					  btn:['确认','取消'],
					  yes:function(index,layero){
						  
						    // var id=$("#id").val();
						     var empNo=$("#empNo").val();
						     var userName=$("#userName").val();
						     var type=$("#type").val();
						     var starTime=$("#starTime").val();
						     var endTime=$("#endTime").val();
						    
						  $.ajax({
							  url:'/addManaVacate',
							  method:'post',
							  contentType:"application/json;charset=utf-8",
							  data:JSON.stringify({empNo:empNo,userName:userName,type:type,starTime:starTime,endTime:endTime}),
							  success:function(data){
								  layer.msg("添加成功");
								  window.setTimeout(function(){window.location.reload();},500)
							  },
							  error:function(data){
								  layer.msg("添加失败");
								  //layer.msg(account1);
							  }
						  });
					  },
					  btn2:function(index){layer.close(index);},
					  success:function(index,layero){
						  layui.use('laydate',function(){
							   var laydate=layui.laydate;
							   laydate.render({
								   elem:'#starTime',
								   type:'date',
							   });
							   laydate.render({
								   elem:'#endTime',
								   type:'date',
							   });
						   });
						  form.render();
					  },
				  });
			  });
		  break;
		  case 'getCheckLength':
			  var data=checkStatus.data;
			  layer.msg('选中了：'+data.length+'个');
		  break;
		  case 'isAll':
			  layer.msg(checkStatus.isAll?'全选':'未全选');
		  break;
		  case 'LAYTABLE_TIPS':
			  layer.alert('这是工具栏右侧自定义的一个图标按钮');
		  break;
		  };
	  });
	  
	  //行工具栏
	  table.on('tool(empInfo)',function(obj){
		  var data=obj.data;
		  if(obj.event==='del'){
			  layer.confirm('真的删除吗',function(index){
				// obj.del();
				 layer.close(index);
					 
				  //向服务器端发送删除指令
				  $.ajax({
					  url:'/delVacate?id='+data.id,
					  method:'post',
					  async:false,
					  
					  success:function(data){
						  if(data){
						  layer.msg("删除成功");
						  obj.del();
						  } 
						//  }else{layer.msg("删除失败");}
					  },
					  error:function(data){
						  if(data){
							  layer.msg("删除失败");
						  }
					  }
				  });
			  });
		  }else if(obj.event==='edit'){
			 layui.use('form',function(){
				 var form=layui.form;
				 var $=layui.jquery;
			 var editIndex=layer.open({
				  formType:1,
				  title:'修改打卡信息',
				  btn:['确认','取消'],
				  yes:function(editIndex,layero){
					  
					//获取表单的值，必须再次出获取，若在给弹窗赋值后再获取，则获取到的仍是原始数据，相当于加载了一个空的表单，然后弹窗赋值，然后获取表单的值
					 var id=$("#id").val();
				     var empNo=$("#empNo").val();
				     var userName=$("#userName").val();
				     var type=$("#type").val();
				     var starTime=$("#starTime").val();
				     var endTime=$("#endTime").val();
					  //向后台发送修改请求
					  $.ajax({
						  url:'/editVacate',
						  contentType:"application/json;charset=utf-8",
						  method:'post',
						  data:JSON.stringify({id:id,empNo:empNo,userName:userName,type:type,starTime:starTime,endTime:endTime}),
						  success:function(data){
							  layer.msg("修改成功");
							 window.setTimeout(function(){window.location.reload();},500)
							 
						  },
						  error:function(data){layer.msg("修改失败");}
					  });
				  },
				  btn2:function(editIndex,layero){
					  layer.close(editIndex);
				  },
				//  end:function(){parent.reload();},
				  area:['450px','380px'],
				  content:$('#editLayer').html(),
				  success:function(layero,index){ 
					  //给个控件赋值
						 $("[name='id']").val(data.id);
						 $("[name='empNo']").val(data.empNo);
						 $("[name='userName']").val(data.userName);
						 $("[name='type']").val(data.type);
						 $("[name='starTime']").val(data.starTime);
						 $("[name='endTime']").val(data.endTime);
						 layui.use('laydate',function(){
							   var laydate=layui.laydate;
							   laydate.render({
								   elem:'#starTime',
								   type:'date',
							   });
							   laydate.render({
								   elem:'#endTime',
								   type:'date',
							   });
						   });
						 form.render()},
				  });						 
			 });
		  }
	  });

  });
</script>
</body>
</html>